﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{ 
    public class DSTWBQI04Data : BaseFunction
    { 
        public string WBBSTKNR = "", WBBART = "", WBBWAKZ = "", WBBFARBE = "", WBBBESCHICH = "", WBBVAR = "", WBBISTKLG = "", WBBSGEW = "", WBBAUFTYP = "", WBBAUFKRS = "", WBBAUFLFD = "", WBBAUFPOS = "";
        public DSTWBQI04Data()
            : base()
        {

        }
        public DSTWBQI04Data(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public DSTWBQI04Data(OracleConnection _myConnection, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_myConnection, _proxy, userID)
        {
           
        }
        /// <summary>
        /// 根据布卷编号获取他本身的品种信息。和当前的品种信息进行比对，如果不同，则将该品种所有的布卷读取出来。 
        ///  </summary>
        /// <param name="WBBSTKNR"></param>
        /// <returns></returns>
        public DataSet GetWBBST(string WBBSTKNR)
        {
            string TableName = "WBBST";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataTable tableLocal = null;
            string KRS="", LFD="", POS="XXX";
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT * "
                + " FROM WBBST W WHERE   WBBMANDANT = '00' "
                + " AND WBBSTKNR=:WBBSTKNR";
                orlReadCommand.Parameters.Add("WBBSTKNR", OracleType.NVarChar).Value = WBBSTKNR;
                //WBBBESCHICH
                setResult = Connect2Proxy(orlReadCommand);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = TableName;
                    tableLocal = setResult.Tables[0];
                    tableLocal.TableName = TableName;
                }
                if (tableLocal.Rows.Count == 0)
                {
                    MessageBox.Show("布卷号没有找到");
                    System.Media.SystemSounds.Question.Play();
                    return setReturn;
                }
                DataRow dwWBBST = tableLocal.Rows[0];
                WBBWAKZ = dwWBBST["WBBWAKZ"].ToString();
                WBBART = dwWBBST["WBBART"].ToString();
                WBBVAR = dwWBBST["WBBVAR"].ToString();
                WBBFARBE = dwWBBST["WBBFARBE"].ToString();
                WBBBESCHICH = dwWBBST["WBBBESCHICH"].ToString();
                WBBAUFTYP = dwWBBST["WBBAUFTYP"].ToString();
                WBBAUFKRS = dwWBBST["WBBAUFKRS"].ToString();
                WBBAUFLFD = dwWBBST["WBBAUFLFD"].ToString();
                WBBAUFPOS = dwWBBST["WBBAUFPOS"].ToString();
                KRS = WBBAUFKRS; LFD = WBBAUFLFD; POS = WBBAUFPOS;
                setResult=GetWBBST();
                setReturn.Merge(setResult);
                if (WBBAUFLFD == "000000")
                {
                    DataRow row = GetMostPossibleVKPOS(WBBWAKZ, WBBART, WBBVAR, WBBFARBE, WBBBESCHICH);
                    if (row != null)
                    {
                        KRS = row["WDAUFKRS"].ToString();
                        LFD = row["WDAUFLFD"].ToString();
                        POS = row["WDAUFPOS"].ToString();
                    } 
                }
                if (POS != "XXX")
                {
                    setResult = ReloadPackingRequirement(KRS, LFD);
                    setReturn.Merge(setResult);                   
                }
                setResult = GetVKPOS(KRS, LFD, POS);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        private DataRow GetMostPossibleVKPOS(string ftype,string art, string var,string color,string finish)
        {
            DataRow row = null;
            try
            {
                 orlReadCommand = OracleDB.CreateCommand();
                 orlReadCommand.CommandText = "SELECT * FROM WDECK WHERE WDWAKZ=:WDWAKZ"+
                 " AND WDART=:WDART AND WDVAR=:WDVAR AND WDFARBE=:WDFARBE AND WDBESCHICH=:WDBESCHICH";
                 orlReadCommand.Parameters.Add("WDWAKZ", OracleType.NVarChar).Value = ftype;
                 orlReadCommand.Parameters.Add("WDART", OracleType.NVarChar).Value = art;
                 orlReadCommand.Parameters.Add("WDVAR", OracleType.NVarChar).Value = var;
                 orlReadCommand.Parameters.Add("WDFARBE", OracleType.NVarChar).Value = color;
                 orlReadCommand.Parameters.Add("WDBESCHICH", OracleType.NVarChar).Value = finish;
                 setResult = Connect2Proxy(orlReadCommand);
                 if (setResult.Tables.Count > 0)
                 {
                     foreach (DataRow dwTemp in setResult.Tables[0].Rows)
                     {
                         row = dwTemp;
                     }
                 } 
               }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return row;
        }
        public DataSet GetWBBST()
        {
            string TableName = "WBBST";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataTable tableLocal = null;
            try
            {

                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT * "
                + " FROM WBBST W WHERE   WBBMANDANT = '00' "
                + " AND WBBWAKZ=:WBBWAKZ"
                + " AND WBBART=:WBBART"
                + " AND WBBVAR=:WBBVAR"
                + " AND WBBFARBE=:WBBFARBE"
                + " AND WBBBESCHICH=:WBBBESCHICH"
                + " AND WBBAUFTYP=:WBBAUFTYP"
                + " AND WBBAUFKRS=:WBBAUFKRS"
                + " AND WBBAUFLFD=:WBBAUFLFD"
                + " AND WBBAUFPOS=:WBBAUFPOS";
                //WBBSTKNR, WBBWAKZ,WBBART ,WBBVAR, WBBFARBE, WBBBESCHICH ,
                //WBBISTKLG ,WBBSGEW, WBBAUFTYP, WBBAUFKRS , WBBAUFLFD , WBBAUFPOS  
                orlReadCommand.Parameters.Add("WBBWAKZ", OracleType.NVarChar).Value = WBBWAKZ;
                orlReadCommand.Parameters.Add("WBBART", OracleType.NVarChar).Value = WBBART;
                orlReadCommand.Parameters.Add("WBBVAR", OracleType.NVarChar).Value = WBBVAR;
                orlReadCommand.Parameters.Add("WBBFARBE", OracleType.NVarChar).Value = WBBFARBE;
                orlReadCommand.Parameters.Add("WBBBESCHICH", OracleType.NVarChar).Value = WBBBESCHICH;
                orlReadCommand.Parameters.Add("WBBAUFTYP", OracleType.NVarChar).Value = WBBAUFTYP;
                orlReadCommand.Parameters.Add("WBBAUFKRS", OracleType.NVarChar).Value = WBBAUFKRS;
                orlReadCommand.Parameters.Add("WBBAUFLFD", OracleType.NVarChar).Value = WBBAUFLFD;
                orlReadCommand.Parameters.Add("WBBAUFPOS", OracleType.NVarChar).Value = WBBAUFPOS;
                setResult = Connect2Proxy(orlReadCommand);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = TableName;
                    tableLocal = setResult.Tables[0];
                    tableLocal.TableName = TableName;
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public void SavePack(DataTable TBPack)
        {
            try
            {
                foreach (DataRow row in TBPack.Rows)
                {
                    orlReadCommand = OracleDB.CreateCommand();
                    orlReadCommand.CommandText = "UPDATE INTEX.WBBST SET WBBBEM=:WBBBEM WHERE WBBSTKNR=:WBBSTKNR";
                    orlReadCommand.Parameters.Add("WBBBEM", OracleType.NVarChar).Value = row["WBBBEM"].ToString();
                    orlReadCommand.Parameters.Add("WBBSTKNR", OracleType.NVarChar).Value = row["WBBSTKNR"].ToString();
                    Connect2Proxy(orlReadCommand);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public DataSet ReloadPackingRequirement(string KRS, string LFD)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
               
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet GetVKPOS(string KRS,string LFD,string POS)
        {

            string TableName = "VKPOS";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT * "
                                                + " FROM VKPOS V  WHERE VKPMANDANT = '00' "
                                                + " AND TRIM(VKPSA) IS NULL AND VKPAUFART ='NA' "
                                                + " AND VKPAUFKRS = :VKPAUFKRS AND VKPAUFLFD = :VKPAUFLFD AND VKPAUFPOS = :VKPAUFPOS";
                orlReadCommand.Parameters.Add("VKPAUFKRS", OracleType.NVarChar).Value = KRS;
                orlReadCommand.Parameters.Add("VKPAUFLFD", OracleType.NVarChar).Value = LFD;
                orlReadCommand.Parameters.Add("VKPAUFPOS", OracleType.NVarChar).Value = POS;
                setResult = Connect2Proxy(orlReadCommand);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = TableName; 
                }
                if (setResult.Tables[0].Rows.Count == 0)
                {
                    DataRow dwVKPOS = setResult.Tables[0].Rows.Add();
                    dwVKPOS["VKPAUFKRS"] = "99";
                    dwVKPOS["VKPAUFLFD"] = System.DateTime.Now.ToFileTime().ToString();
                    dwVKPOS["VKPAUFPOS"] = "XXX"; 
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }  
    }
}